<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
/**
 * 帐套控制器
 */
class AccountController extends CommonController{
	/**
	 * 帐套列表
	 */
	public function index(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'id';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);
		$db=M('Account');
		//获取总数据条数
		$count=$db->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where(array('del'=>'0'))->select();
		//权限菜单的显示与隐藏	
		$this->assign('account_addId',83);//添加
		$this->assign('account_listId',84);//删除
		$this->display();

	}

	public function add(){
		if(!empty($_POST)){
			$sql="SELECT nextval('SEQ_ZT') seq";
			$db=M('account');
			$seq=($db->query($sql));
			$seq='ZT'.$seq[0]['seq'];
			$data=array(
				'sequence'=>$seq,
				'createtime'=>time(),
				'uid'=>session('uid')
				);
			$account_id=$db->add($data);
			if($account_id){
				$Dao = M(); 
				//将当天库存表复制添加到帐套表中
				$copySQL="insert into think_book(code_code,begin,income,pay,movein,moveout,loss,refund,account_id) select code_code,begin,income,pay,movein,moveout,loss,refund,'".$account_id."' from think_number";
		    	$book = $Dao->execute($copySQL);
		    	if($book){
		    	//更新库存表期初数值
			    	$sqls="update think_number set begin = begin + income - pay + movein - moveout + loss - refund,income = 0,pay = 0,moveout = 0,movein = 0,refund = 0,loss = 0";
			    	$num=$Dao->execute($sqls);
			    	//清除数据小于等于0 的商品库存信息
			    	if($num){
			    		$del=$Dao->execute("delete from think_number where begin <= 0 ");
			    		$export=$Dao->execute("update think_export set status= 2,account_id = ".$account_id." where account_id = 0 ");
			    		$storage=$Dao->execute("update think_storage set status= 2,account_id = ".$account_id." where account_id = 0 ");
			    		$loss=$Dao->execute("update think_loss set status= 2,account_id = ".$account_id." where account_id = 0 ");
			    		$allot=$Dao->execute("update think_allot set status= 2,account_id = ".$account_id." where account_id = 0 ");
			    		$refund=$Dao->execute("update think_refund set status= 2,account_id = ".$account_id." where account_id = 0 ");
			    		$this->success('添加成功');
			    	}else{
			    		$this->error('新的物料库存表初始化失败');
			    	}
			    }else{
			    	$this->error('帐套明细表增加失败');
			    }
			}else{
				$this->error('新的帐套编码添加失败');
			}
		}else{
			//查询出库记录次数
			$sql="select count(*) as count from think_export where account_id = 0 and del=0";
			$Dao = M();
			$ConutAll=$Dao->query($sql);
			$count=array();
			$count['export'][0]=$ConutAll[0]['count'];
			$sql="select count(*) as count from think_export where account_id = 0 and del=0 and status = 1";
			$Conut0=$Dao->query($sql);
			$count['export'][1]=$Conut0[0]['count'];
			//查询人库记录次数
			$sql="select count(*) as count from think_storage where account_id = 0 and del=0";
			$ConutAll=$Dao->query($sql);
			$count['storage'][0]=$ConutAll[0]['count'];
			$sql="select count(*) as count from think_storage where account_id = 0 and del=0 and status = 1";
			$Conut0=$Dao->query($sql);
			$count['storage'][1]=$Conut0[0]['count'];
			//查询退货记录次数
			$sql="select count(*) as count from think_refund where account_id = 0 and del=0";
			$ConutAll=$Dao->query($sql);
			$count['refund'][0]=$ConutAll[0]['count'];
			$sql="select count(*) as count from think_refund where account_id = 0 and del=0 and status = 1";
			$Conut0=$Dao->query($sql);
			$count['refund'][1]=$Conut0[0]['count'];
			//查询损溢记录次数
			$sql="select count(*) as count from think_loss where account_id = 0 and del=0";
			$ConutAll=$Dao->query($sql);
			$count['loss'][0]=$ConutAll[0]['count'];
			$sql="select count(*) as count from think_loss where account_id = 0 and del=0 and status = 1";
			$Conut0=$Dao->query($sql);
			$count['loss'][1]=$Conut0[0]['count'];
			//查询调拨记录次数
			$sql="select count(*) as count from think_allot where account_id = 0 and del=0";
			$ConutAll=$Dao->query($sql);
			$count['allot'][0]=$ConutAll[0]['count'];
			$sql="select count(*) as count from think_allot where account_id = 0 and del=0 and status = 1";
			$Conut0=$Dao->query($sql);
			$count['allot'][1]=$Conut0[0]['count'];
			$this->assign('count',$count);
			$this->commit=$count['export'][1]+$count['storage'][1]+$count['refund'][1]+$count['allot'][1]+$count['loss'][1];
			$this->display();
		}

	}
	public function showList(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'id';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);
		//加载检索的基础数据
		$this->search();
		//检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        	$where=$this->_search();
        }
        //get方式查看入库明细
        if(!empty($_GET)){
        	if(isset($_GET['id'])){
        		$where['account_id']=array('eq',$_GET['id']);
        		$search_where['account_id']['eq']=$_GET['id'];
        		$search_where['account_sequence']['eq']=$_GET['sequence'];
        		$this->assign('search_where',$search_where);
        	}
        }

    	//在where条件中增加未删除的判断条件
       	unset($where['goods_name']);
       	unset($where['account_sequence']);
		$db=D('BookView');
		//获取总数据条数
		$count=$db->where($where)->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$field=array('depot_name','supplier_name','code','goods_name','xinghao','guige','unit','price','begin','income','pay','movein','moveout','loss','refund','account_id','(begin + income - pay + movein - moveout + loss - refund) as final');
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->field($field)->select();
		$this->display();
	}

	public function lookup(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'id';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'asc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);

        //检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        }
        $where=$this->_search();
        //在where条件中增加未删除的判断条件
		$db=M('account');
		//获取总数据条数
		$count=$db->where($where)->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->select();
		$this->display();
	}
}